package com.ddm.controller.web;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ddm.dto.page.PageQueryDTO;
import com.ddm.entity.webEntity.Regulation;
import com.ddm.result.PageResult;
import com.ddm.result.Result;
import com.ddm.service.webService.RegulationService;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hpq
 * @version 1.0
 */
@RestController("WebRegulationController")
@RequestMapping("/Web/regulation")
@Api(tags = "规章制度相关接口")
@Slf4j
public class RegulationController {

    @Autowired
    private RegulationService regulationService;

    @GetMapping
    @ApiOperation("查询所有规章制度文档")
    public Result<List<Regulation>> list() {
        log.info("查询所有规章制度文档");
        List<Regulation> regulations = regulationService.getAllRegulations();
        return Result.success(regulations);
    }

    @GetMapping("/findById")  // 统一改为查询参数方式
    @ApiOperation("根据id查询规章制度文档")  // 修正注解描述，与功能一致
    public Result<Regulation> getById(
            @ApiParam(value = "规章制度id", required = true)
            @RequestParam(required = false) Integer id  // 保持与课程接口一致的参数传递方式
    ) {
        log.info("根据id查询规章制度文档:{}", id);
        Regulation regulation = regulationService.getRegulationById(id);
        return Result.success(regulation);
    }

    @PostMapping
    @ApiOperation("添加规章制度")
    public Result<Regulation> add(
            @RequestBody Regulation regulation
    ) {
        Boolean result = regulationService.add(regulation);
        return result ? Result.success(regulation) : Result.error("添加失败");
    }

    @PutMapping
    @ApiOperation("更新规章制度")
    public Result<Regulation> update(@RequestBody Regulation regulation) {
        if (regulation.getId() == null) {
            return Result.error("更新失败，id不能为空");
        }
       regulationService.update(regulation);
        return Result.success(regulation);
    }

    @DeleteMapping("/delete")  // 统一改为查询参数方式
    @ApiOperation("删除规章制度")
    public Result<Regulation> delete(
            @ApiParam(value = "规章制度id", required = true)
            @RequestParam(required = false) Integer id  // 保持与课程接口一致的参数传递方式
    ) {
        Regulation deletedRegulation = regulationService.getRegulationById(id);  // 先查询再删除
        regulationService.delete(id);
        return  Result.success(deletedRegulation);
    }


    @GetMapping("/pageAll")
    @ApiOperation("分页查询规章制度")
    public Result<PageResult> getRegulationsByPage(
            @ApiParam(value = "当前页码", required = false, defaultValue = "1")
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,

            @ApiParam(value = "每页记录数", required = false, defaultValue = "10")
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @ApiParam(value = "关键词（模糊查询）", required = false)
            @RequestParam(required = false) String key
    ) {
        Page<Regulation> page = new Page<>(pageNum, pageSize);
        IPage<Regulation> regulationPage = regulationService.getRegulationsByPage(page,key);

        return Result.success(new PageResult(
                regulationPage.getTotal(),
                regulationPage.getRecords()
        ));
    }

    @GetMapping("/page")
    @ApiOperation("根据分类ID分页查询法规信息")
    public Result<PageResult> getRegulationsByCategoryIds(
            @ApiParam(value = "分类ID列表（多个ID用 &ids= 拼接）", required = true)
            @RequestParam("ids") List<Integer> ids,
            @ApiParam(value = "当前页码", required = false, defaultValue = "1")
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = false, defaultValue = "10")
            @RequestParam(required = false, defaultValue = "10") Integer pageSize
    ) {
        log.info("根据分类ID分页查询法规信息，分类ID：{}，页码：{}，每页大小：{}",
                ids, pageNum, pageSize);

        Page<Regulation> page = new Page<>(pageNum, pageSize);
        IPage<Regulation> regulationPage = regulationService.getRegulationsByCategoryIds(ids, page);

        PageResult pageResult = new PageResult(
                regulationPage.getTotal(),
                regulationPage.getRecords()
        );return Result.success(pageResult);}

    @GetMapping("/pageByUserId")
    @ApiOperation("根据用户ID分页查询收藏的法规信息")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<PageResult> getRegulationByPageAndUser(
            //添加用户id
            @ApiParam(value = "用户id", defaultValue = "1")
            @RequestParam(required = true) Integer userId,
            @ApiParam(value = "当前页码", required = false, defaultValue = "1")
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = false, defaultValue = "10")
            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
            @ApiParam(value = "关键词（模糊查询）", required = false)
            @RequestParam(required = false) String key)
    {
        Page<Regulation> page = new Page<>(pageNum, pageSize);
        // 调用新增的 service 方法，同时传入 userId 和 key
        IPage<Regulation> regulationPage = regulationService.getCollectedRegulationByUserId(page, userId, key);

        // 使用 PageResult 封装分页信息
        PageResult pageResult = new PageResult(
                regulationPage.getTotal(),      // 总记录数
                regulationPage.getRecords()     // 当前页数据
        );

        return Result.success(pageResult);
    }
    }

